﻿Imports System.Data.OleDb

Public Class SiteManager
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        DrawPieChart_RecCategories()
        DrawLineChart_SiteViews()
        DrawMap_UsersByCountry()
    End Sub

    Private Sub DrawPieChart_RecCategories()

        Dim wtChart As String = "<script>"
        wtChart += "google.load('visualization', '1.1', { 'packages': ['corechart'] });"

        wtChart += "google.setOnLoadCallback(drawChart);"

        wtChart += "function drawChart() {"
        wtChart += "var data = new google.visualization.DataTable();"
        wtChart += "data.addColumn('string', 'Type_ID');"
        wtChart += "data.addColumn('number', 'NumTypes');"
        wtChart += "data.addRows(["

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("recipeConnectionString").ConnectionString)
        Dim SqlString As String = "SELECT * from STAT_RECIPE_BY_TYPE_VIEW"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        oleDbConn.Open()

        Dim dr As OleDbDataReader = cmd.ExecuteReader

        If dr.HasRows Then
            Do While dr.Read()
                wtChart += "['" + (dr("TYPE_DESC").ToString) + "', " + (dr("NUMTypes").ToString) + "],"
            Loop
        End If

        dr.Close()

        wtChart = wtChart.Substring(0, wtChart.Length - 1)

        wtChart += "]);"
        wtChart += "var options = { 'title': '', 'width': 800, 'height': 400, is3D: true };"
        wtChart += "var chart = new google.visualization.PieChart(document.getElementById('div_recCategories'));"
        wtChart += "chart.draw(data, options);"

        wtChart += "}"
        wtChart += "</script>"
        ltr_Pie_RecCategories.Text = wtChart

    End Sub


    Private Sub DrawLineChart_SiteViews()

        Dim lnChart As String = "<script>"
        lnChart += "google.load('visualization', '1', { 'packages': ['corechart'] });"

        lnChart += "google.setOnLoadCallback(drawChart);"

        lnChart += "function drawChart() {"
        lnChart += "var data = google.visualization.arrayToDataTable(["
        lnChart += "['Month', 'Views'],"

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("recipeConnectionString").ConnectionString)
        Dim SqlString As String = "SELECT * from STAT_RECIPEVIEWED_MONTH"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        oleDbConn.Open()

        Dim dr As OleDbDataReader = cmd.ExecuteReader

        If dr.HasRows Then
            Do While dr.Read()
                lnChart += "['" + (dr("RECVIEW_MTHYR").ToString) + "', " + (dr("RecV").ToString) + "],"
            Loop
        End If

        dr.Close()

        lnChart += "]);"
        lnChart += "var options = { 'title': '', 'width': 960, 'height': 400, 'fontSize': 12, hAxis: {slantedText:true, slantedTextAngle:45}, vAxis: {minValue: 0},};"
        lnChart += "var chart = new google.visualization.LineChart(document.getElementById('div_siteViews'));"
        lnChart += "chart.draw(data, options);"

        lnChart += "}"
        lnChart += "</script>"
        ltr_SiteViews.Text = lnChart

    End Sub



    Private Sub DrawMap_UsersByCountry()

        Dim lnChart As String = "<script>"
        lnChart += "google.load('visualization', '1', { 'packages': ['geochart'] });"

        lnChart += "google.setOnLoadCallback(drawRegionsMap);"

        lnChart += "function drawRegionsMap() {"
        lnChart += "var data = google.visualization.arrayToDataTable(["
        lnChart += "['Country', 'Registered Users'],"

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("recipeConnectionString").ConnectionString)
        Dim SqlString As String = "SELECT * from STAT_USERS_BY_COUNTRY"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        oleDbConn.Open()

        Dim dr As OleDbDataReader = cmd.ExecuteReader

        If dr.HasRows Then
            Do While dr.Read()
                lnChart += "['" + (dr("CTRY_NAME").ToString) + "', " + (dr("TotalPerCountry").ToString) + "],"
            Loop
        End If

        dr.Close()

        lnChart += "]);"
        lnChart += "var options = {};"
        lnChart += "var chart = new google.visualization.GeoChart(document.getElementById('div_usersCountry'));"
        lnChart += "chart.draw(data, options);"

        lnChart += "};"
        lnChart += "</script>"
        ltr_UsersCountry.Text = lnChart

    End Sub

End Class